home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2.sit
/
Raven 1.2
/
Source
/
Foundation
/
Common
/
ZMemoryHeap.h
< prev
next >
Wrap
Text File
|
1997-08-16
|
5KB
|
197 lines
/*
* File: ZMemoryHeap.h
* Summary: A class that uses a TAllocator and zero or more TFixedAllocators
* to allocate blocks of memory.
* Written by: Jesse Jones
*
* Copyright ゥ 1997 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <2> 8/15/97 JDJ Block count functions are defined if !RELEASE (instead
* of if DEBUG).
* <1> 1/29/97 JDJ Created
*/
#pragma once
#include <Files.h>
#include <New.h>
#include <ZTypes.h>
//-----------------------------------
// Forward References
//
struct SMemoryBlock;
class TAllocator;
class TFixedAllocator;
// ===================================================================================
// class TMemoryHeap
// This class is intended to be the principal way to allocate smallish blocks:
// the allocators are simply helper classes. Although TAllocator and TMemoryHeap
// have very similar interfaces I think the conceptual differences are large
// enough to make using a common base class questioanble.
// ===================================================================================
class TMemoryHeap {
friend class TDisableLeakChecking;
//-----------------------------------
// Initialization/Destruction
//
public:
~TMemoryHeap();
TMemoryHeap(TAllocator* takeAllocator);
private:
TMemoryHeap(const TMemoryHeap& rhs);
TMemoryHeap& operator=(const TMemoryHeap& rhs);
//-----------------------------------
// Allocations
//
public:
void* Allocate(ulong bytes);
// Returns nil if allocation failed.
void Deallocate(void* block);
//-----------------------------------
// Fixed Size Allocators
//
public:
void AddAllocator(ulong blockSize, ulong maxBlocks);
const TFixedAllocator* GetAllocator(ulong size) const;
//-----------------------------------
// Info
//
public:
// ----- Heap -----
ulong GetHeapSize() const;
ulong GetPoolCount() const;
// Returns the number of pools allocated (after the initial pool).
// To avoid Memory Manager fragmentation this should be as small
// as possible.
ulong GetCurrentBytes() const {return mCurrentBytes;}
// Returns the current number of bytes allocated in the heap.
ulong GetHighwaterMark() const {return mHighwater;}
// Returns the largest number of bytes ever allocated by the heap.
ulong GetDebugOverhead() const {return 100*mDebugOverhead/mCurrentBytes;}
// Returns the percent of the current number of allocated bytes used
// by debugging fields.
// ----- Blocks -----
#if !RELEASE
ulong GetBlockCount() const {return mBlockCount;}
// Returns the current number of allocated blocks.
ulong GetTotalBlockCount(ulong size) const;
// Returns the number of blocks that were ever allocated with the given size.
ulong GetCurrentBlockCount(ulong size) const;
// Returns the number of currently allocated blocks with the given size.
void DumpCommonBlocks();
// Displays an alert containing statistics on the most commonly allocated
// block sizes.
void DumpAllocatorCapacities();
// Displays an alert containing statistics on the fixed allocator capacities.
#endif
ulong GetBlockSize(const void* ptr) const;
// Note that this may be slightly larger than the size passed to
// Allocate.
//-----------------------------------
// Debugging
//
public:
#if DEBUG
// ----- Zapping -----
bool IsZapping() const;
// Returns true if new and deleted blocks are being zapped.
void SetZap(bool zap);
// Zapping defaults to on if DEBUG is true.
// ----- Validation -----
void ValidateBlock(const void* ptr) const;
void ValidateHeap() const;
// ----- Leak Checking -----
bool IsLeakChecking() const;
bool IsLeakChecked(const void* ptr) const;
ulong GetLeakCount() const {return mLeakCount;}
void DumpLeaks() const;
#endif
//-----------------------------------
// Internal API
//
public:
static void* operator new(size_t size);
// Memory heaps can be used by the global operator new so we'll
// take care of allocating ourselves.
static void operator delete(void* ptr);
protected:
ulong GetTotalBlockSize(const void* ptr) const;
#if DEBUG
void AddDebugInfo(SMemoryBlock* block, ulong actualBytes, ulong size);
FSSpec GetLeaksLog() const;
#endif
//-----------------------------------
// Member Data
//
protected:
TAllocator* mAllocator;
TFixedAllocator* mFixedAllocators[256];
ulong mBlockCount;
ulong mLeakCount;
ulong mHighwater;
ulong mCurrentBytes;
ulong mDebugOverhead;
#if DEBUG
bool mZap;
long mLeakChecking;
#endif
#if !RELEASE
ulong mTotalBlockCounts[256];
ulong mCurrentBlockCounts[256];
#endif
};
// ===================================================================================
// Inlines
// ===================================================================================
#if DEBUG
inline bool TMemoryHeap::IsZapping() const {return mZap;}
inline void TMemoryHeap::SetZap(bool zap) {mZap = zap;}
inline bool TMemoryHeap::IsLeakChecking() const {return mLeakChecking > 0;}
#endif